home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / overview / dtscpluslibrary / sources / memoryclass.cp < prev    next >
Encoding:
Text File  |  2000-06-23  |  3.9 KB  |  164 lines

  1. /*
  2.     File:        MemoryClass.cp
  3.  
  4.     Contains:    TMemory is a simple object checks heap and stack values, as well as changes them.
  5.                   TMemoryClass.cp contains the TMemory member function definitions.
  6.  
  7.     Written by: Kent Sandvik    
  8.  
  9.     Copyright:    Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 8/18/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24. #ifndef _MEMORYCLASS_
  25. #include "MemoryClass.h"
  26. #endif
  27.  
  28.  
  29. // CONSTRUCTORS AND DESTRUCTORS
  30. #pragma segment Memory
  31. TMemory::TMemory(const long minHeap,
  32.                  const long minStack)
  33. // The one and only constructor, takes min values for stack and heap if wanted, but
  34. // these are not needed.
  35. {
  36.     fMinHeap = minHeap;                            // minimum heap size
  37.     fMinStack = minStack;                        // minimum stack size
  38. }
  39.  
  40.  
  41. #pragma segment Memory
  42. TMemory::~TMemory()
  43. // Default constructor -- unused for the time being.
  44. {
  45. }
  46.  
  47.  
  48. // MAIN INTERFACE
  49. #pragma segment Memory
  50. long TMemory::GetStackSize()
  51. // Get size of the current stack.
  52. {
  53.     return ::StackSpace();                        // return size of stack
  54. }
  55.  
  56.  
  57. #pragma segment Memory
  58. Boolean TMemory::SetStackSize(const long stackValue)
  59. // Set absolute size of current stack.
  60. {
  61.     long oldValue = ::StackSpace();
  62.  
  63.     // Set the new value
  64.     SetApplLimit((Ptr)((long)GetApplLimit() - (stackValue - oldValue)));
  65.  
  66.     fError = MemError();
  67.     VASSERT(fError == noErr, ("Problems with SetApplLimit = %d", fError));
  68.  
  69.     if (fError != noErr)
  70.         return false;
  71.     else
  72.         return true;
  73. }
  74.  
  75.  
  76. #pragma segment Memory
  77. Boolean TMemory::SetStackSizeFromResources()
  78. // Get stack values from pre-defined resource (that should be part of the application).
  79. {
  80.     long newStackSize = 0;
  81.  
  82.     short num = ::CountResources(kStackResource);// find out how many we got
  83.     if (num == 0)                                // none?
  84.         goto FromResourceFalse;
  85.     else
  86.     {
  87.         for (int i = 1; i <= num; ++i)
  88.         {
  89.             Handle temp = ::GetIndResource(kStackResource, i);
  90.             fError = ResError();
  91.             VASSERT(fError == noErr, ("Problems with GetIndResource = %d", fError));
  92.             {
  93.                 const StackResource& stackValueH = **((StackValueHandle)temp);
  94.                 newStackSize += stackValueH.stackVal;
  95.             }
  96.             if (temp != NULL)
  97.                 ::ReleaseResource(temp);
  98.         }
  99.     }
  100.     this->SetStackSize(newStackSize);            // set the new stack size
  101.     return true;
  102.  
  103. FromResourceFalse:return false;
  104. }
  105.  
  106.  
  107. #pragma segment Memory
  108. Boolean TMemory::IncreaseStackSize(const long stackValue)
  109. // Increase the stack value from the current size.
  110. {
  111.     ::SetApplLimit((Ptr)((long)GetApplLimit() - stackValue));
  112.  
  113.     fError = MemError();
  114.     VASSERT(fError == noErr, ("Problems with SetApplLimit = %d", fError));
  115.  
  116.     if (fError != noErr)
  117.         return false;
  118.     else
  119.         return true;
  120. }
  121.  
  122.  
  123. #pragma segment Memory
  124. Boolean TMemory::CheckStackSize()
  125. // Check if we hit the low watermark of the stack size.
  126. {
  127.     long currentStack = ::StackSpace();
  128.  
  129.     if (currentStack < fMinStack)
  130.         return false;
  131.     else
  132.         return true;
  133. }
  134.  
  135.  
  136. #pragma segment Memory
  137. Boolean TMemory::CheckHeapSize()
  138. // Check if we hit the low watermark of the heap size.
  139. {
  140.     long currentHeap = this->GetHeapSize();
  141.  
  142.     if (currentHeap < fMinHeap)
  143.         return false;
  144.     else
  145.         return true;
  146. }
  147.  
  148.  
  149. #pragma segment Memory
  150. long TMemory::GetHeapSize()
  151. // Get size of current heap.
  152. {
  153.     return ((long)GetApplLimit() - (long)ApplicationZone());
  154. }
  155.  
  156.  
  157. // _________________________________________________________________________________________________________ //
  158.  
  159. /*    Change History (most recent last):
  160.   No        Init.    Date        Comment
  161.   1            khs        1/2/93        New file
  162.   2            khs        1/3/93        Cleanup
  163. */
  164.